package jiangyeheng.forth;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        Student stu1 = new Student(78.0, 89.0, 87.0, "小明");
        Student stu2 = new Student(87.0, 98.0, 19.0, "小李");
        Student stu3 = new Student(65.0, 26.0, 91.0, "小白");
        Student stu4 = new Student(24.0, 73.0, 82.0, "小壮");
        Student stu5 = new Student(100.0, 100.0, 100.0, "狗四");

        ArrayList<Student> students = new ArrayList<>();
        students.add(stu1);
        students.add(stu2);
        students.add(stu3);
        students.add(stu4);
        students.add(stu5);

        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {

                double totalCompare = s2.sum() - s1.sum();
                if (totalCompare != 0) {
                    return totalCompare > 0 ? 1 : -1;
                }

                double chineseCompare = s2.chineseScore - s1.chineseScore;
                if (chineseCompare != 0) {
                    return chineseCompare > 0 ? 1 : -1;
                }

                double mathCompare = s2.mathScore - s1.mathScore;
                if (mathCompare != 0) {
                    return mathCompare > 0 ? 1 : -1;
                }

                return 0;
            }
        });



        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\t%.1f\n",
                    i + 1, student.name, student.chineseScore,
                    student.mathScore, student.englishScore, student.sum());
        }


    }


}

class Student {
    public String name;
    public double chineseScore;
    public double mathScore;
    public double englishScore;

    public Student(double chineseScore, double mathScore, double englishScore, String name) {
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
        this.name = name;
    }

    public double sum() {
        return chineseScore + mathScore + englishScore;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;

        Student other = (Student) obj;
        if (Math.abs(this.sum() - other.sum()) > 0.001) {
            return false;
        }

        if (Math.abs(this.chineseScore - other.chineseScore) > 0.001) {
            return false;
        }

        if (Math.abs(this.mathScore - other.mathScore) > 0.001) {
            return false;
        }

        return true;
    }


    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + Double.hashCode(sum());
        result = 31 * result + Double.hashCode(chineseScore);
        result = 31 * result + Double.hashCode(mathScore);
        return result;
    }

    @Override
    public String toString() {
        return String.format("Student{name='%s', 语文=%.1f, 数学=%.1f, 英语=%.1f, 总分=%.1f}",
                name, chineseScore, mathScore, englishScore, sum());
    }
}